//-----------------------------------------------------------------------
// <copyright file="a_StatsByDateDAOBase.cs" company="AACECOM">
//     Copyright (c) AACECOM. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ThoiTrang.Entities;

namespace ThoiTrang.Data
{    
    public abstract class a_StatsByDateDAOBase
    {
        #region Common methods
        public virtual a_StatsByDate Createa_StatsByDateFromReader(IDataReader reader)
        {
            a_StatsByDate item = new a_StatsByDate();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) item.ID = (int)reader["ID"];
                if (!reader.IsDBNull(reader.GetOrdinal("EventDate"))) item.EventDate = (DateTime)reader["EventDate"];
                if (!reader.IsDBNull(reader.GetOrdinal("DocType"))) item.DocType = (byte)reader["DocType"];
                if (!reader.IsDBNull(reader.GetOrdinal("EventType"))) item.EventType = (byte)reader["EventType"];
                if (!reader.IsDBNull(reader.GetOrdinal("DocID"))) item.DocID = (int)reader["DocID"];
                if (!reader.IsDBNull(reader.GetOrdinal("Counts"))) item.Counts = (int)reader["Counts"];
            }
            catch (Exception ex)
            {                
                throw ex;
            }
            return item;
        }
        #endregion
        
        #region Createa_StatsByDate methods
            
        public virtual void Createa_StatsByDate(a_StatsByDate a_StatsByDate)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_StatsByDate_ins");
                
                database.AddInParameter(dbCommand, "@EventDate", DbType.DateTime, a_StatsByDate.EventDate);
                database.AddInParameter(dbCommand, "@DocType", DbType.Byte, a_StatsByDate.DocType);
                database.AddInParameter(dbCommand, "@EventType", DbType.Byte, a_StatsByDate.EventType);
                database.AddInParameter(dbCommand, "@DocID", DbType.Int32, a_StatsByDate.DocID);
                database.AddInParameter(dbCommand, "@Counts", DbType.Int32, a_StatsByDate.Counts);
                database.AddOutParameter(dbCommand, "@ID", DbType.Int32, 0);
                
                database.ExecuteNonQuery(dbCommand);
                a_StatsByDate.ID = (int)database.GetParameterValue(dbCommand, "@ID");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Updatea_StatsByDate methods
        
        public virtual void Updatea_StatsByDate(a_StatsByDate a_StatsByDate)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_StatsByDate_upd");            
                
                database.AddInParameter(dbCommand, "@ID", DbType.Int32, a_StatsByDate.ID);
                database.AddInParameter(dbCommand, "@EventDate", DbType.DateTime, a_StatsByDate.EventDate);
                database.AddInParameter(dbCommand, "@DocType", DbType.Byte, a_StatsByDate.DocType);
                database.AddInParameter(dbCommand, "@EventType", DbType.Byte, a_StatsByDate.EventType);
                database.AddInParameter(dbCommand, "@DocID", DbType.Int32, a_StatsByDate.DocID);
                database.AddInParameter(dbCommand, "@Counts", DbType.Int32, a_StatsByDate.Counts);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }
        
        #endregion

        #region Deletea_StatsByDate methods
        public virtual void Deletea_StatsByDate(int id)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_StatsByDate_del");
                
                database.AddInParameter(dbCommand, "@ID", DbType.Int32, id);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }

        #endregion

        #region Geta_StatsByDate methods
        
        public virtual a_StatsByDate Geta_StatsByDate(int id)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_StatsByDate_sel");
                
                database.AddInParameter(dbCommand, "@ID", DbType.Int32, id);
                
                a_StatsByDate a_StatsByDate =  null;
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    if (reader.Read())
                    {
                        a_StatsByDate = Createa_StatsByDateFromReader(reader);
                        reader.Close();
                    }
                }
                return a_StatsByDate;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        #endregion

        #region Geta_StatsByDateList methods
        public virtual a_StatsByDateCollection Geta_StatsByDateList(a_StatsByDateColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_StatsByDate_selList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                a_StatsByDateCollection a_StatsByDateCollection = new a_StatsByDateCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_StatsByDate a_StatsByDate = Createa_StatsByDateFromReader(reader);
                        a_StatsByDateCollection.Add(a_StatsByDate);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return a_StatsByDateCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        public virtual a_StatsByDateCollection Geta_StatsByDateList(a_StatsByDateColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return Geta_StatsByDateList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
    }
}